<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <el-input
            v-model="searchForm.title"
            placeholder="活动"
            clearable
        >
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="getActivityList">搜索</el-button>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="dialogVisible = true" v-if="hasAuth('sys:activity:save')">添加活动</el-button>
      </el-form-item>

      <el-form-item>
        <el-popconfirm title="这是确定批量删除吗？" @confirm="delHandle(null)">
          <el-button type="danger" slot="reference" :disabled="delBtlstatus" v-if="hasAuth('sys:activity:delete')">
            批量删除
          </el-button>
        </el-popconfirm>
      </el-form-item>

      <el-form-item>
        <el-button type="info" @click="getStatistics = true" v-if="hasAuth('sys:activity:statistics')">统计</el-button>
      </el-form-item>

    </el-form>

    <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        border
        stripe
        @selection-change="handleSelectionChange">

      <el-table-column
          type="selection"
          width="55">
      </el-table-column>

      <el-table-column
          prop="title"
          label="标题"
          width="50">
      </el-table-column>

      <el-table-column
          prop="context"
          label="主要内容"
          width="120">
      </el-table-column>

      <el-table-column
          prop="image"
          label="图片"
          width="100">
        <template slot-scope="scope">
          <img :src="'http://localhost:9000/api/file/' +scope.row.image" style="width: 80px; margin-top: 10px;"
               alt="图片">
        </template>
      </el-table-column>
      <el-table-column
          prop="address"
          label="地址">
      </el-table-column>
      <el-table-column
          prop="endTime"
          label="截止时间">
      </el-table-column>

      <el-table-column
          prop="status"
          label="状态"
          width="80">
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.status === 0" type="warning">未审核</el-tag>
          <el-tag size="small" v-else-if="scope.row.status === 1" type="success">已审核</el-tag>
          <el-tag size="small" v-else-if="scope.row.status === 2" type="danger">未通过</el-tag>
        </template>

      </el-table-column>
      <el-table-column
          prop="icon"
          width="350px"
          label="操作">

        <template slot-scope="scope">

          <el-button type="text" @click="viewHandle(scope.row.id)">查看</el-button>
          <el-divider direction="vertical"></el-divider>

          <el-button type="text" @click="updateHandle(scope.row.id)">修改</el-button>
          <el-divider direction="vertical"></el-divider>

          <el-button type="text" @click="auditHandle(scope.row.id)">审核</el-button>
          <el-divider direction="vertical"></el-divider>

          <el-button type="text" @click="applicationHandle(scope.row.id)">报名</el-button>
          <el-divider direction="vertical"></el-divider>

          <el-button type="text" @click="downloadHandle(scope.row.id)">导出</el-button>
          <el-divider direction="vertical"></el-divider>

          <el-button type="text" @click="evaluateHandle(scope.row.id)">评价</el-button>
          <el-divider direction="vertical"></el-divider>


          <template>
            <el-popconfirm title="确定删除该活动吗？" @confirm="delHandle(scope.row.id)">
              <el-button type="text" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 20, 50, 100]"
        :current-page="current"
        :page-size="size"
        :total="total">
    </el-pagination>

    <!-- 新增对话框 -->
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="600px"
        :before-close="handleClose">
      <el-form :model="editForm" :rules="editFormActivitys" ref="editForm">

        <el-form-item label="标题" prop="title" label-width="100px">
          <el-input v-model="editForm.title" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="主要内容" prop="context" label-width="100px">
          <el-input v-model="editForm.context" type="textarea" :autosize="{ minRows: 4, maxRows: 10 }"></el-input>
        </el-form-item>

        <!-- 限制该活动的参与人数 -->
        <el-form-item label="参与人数" prop="joinCount" label-width="100px">
          <el-input v-model="editForm.limitCount" type="number" min="1" max="100000000"></el-input>
        </el-form-item>

        <el-form-item label="地址" prop="address" label-width="100px">
          <el-input v-model="editForm.address" autocomplete="off"></el-input>
        </el-form-item>

        <!-- 活动开始时间 -->
        <el-form-item label="开始时间" prop="startTime" label-width="100px">
          <el-date-picker type="date" placeholder="选择日期" v-model="editForm.startTime"
                          style="width: 100%;"></el-date-picker>
        </el-form-item>

        <!-- 活动结束时间 -->
        <el-form-item label="结束时间" prop="endTime" label-width="100px">
          <el-date-picker type="date" placeholder="选择日期" v-model="editForm.endTime"
                          style="width: 100%;"></el-date-picker>
        </el-form-item>

        <el-form-item label="图片" prop="image" label-width="100px">
          <el-upload
              class="upload-demo"
              :show-file-list="true"
              action="http://localhost:9000/sys/activity/uploadImage"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :on-exceed="handleExceed"
              :before-remove="beforeRemove"
              :file-list="editForm.image"
              multiple
              :limit="3"
              :on-success="handleSuccess">
            <!-- 构建虚拟路径 -->
            <img v-if="editForm.image" :src=" 'http://localhost:9000/api/file/' + editForm.image" class="avatar">
            <el-button v-else class="el-icon-plus avatar-uploader-icon">点击上传</el-button>
          </el-upload>
        </el-form-item>

        <el-form-item style="display: flex; justify-content: center;">
          <el-button type="primary" @click="submitForm('editForm')">立即发布</el-button>
          <el-button @click="resetForm('editForm')">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 查看活动对话框 -->
    <el-dialog
        title="查看活动"
        :visible.sync="viewDialogVisible"
        width="600px" :before-close="handleViewClose">
      <el-form :model="viewForm" :rules="editFormActivitys" ref="viewForm">

        <el-form-item label="标题" prop="title" label-width="100px">
          <el-input v-model="viewForm.title" disabled></el-input>
        </el-form-item>

        <el-form-item label="主要内容" prop="context" label-width="100px">
          <el-input v-model="viewForm.context" type="textarea" :autosize="{ minRows: 4, maxRows: 10 }"
                    disabled></el-input>
        </el-form-item>

        <el-form-item label="地址" prop="address" label-width="100px">
          <el-input v-model="viewForm.address" disabled></el-input>
        </el-form-item>

        <el-form-item label="图片" prop="image" label-width="100px" width="100px">
          <img v-if="viewForm.image" :src=" 'http://localhost:9000/api/file/' + viewForm.image" class="avatar">
        </el-form-item>

        <el-form-item label="创建时间" prop="created" label-width="100px">
          <el-input v-model="viewForm.created" disabled></el-input>
        </el-form-item>

        <!-- 活动开始时间 -->
        <el-form-item label="开始时间" prop="created" label-width="100px">
          <el-input v-model="viewForm.startTime" disabled></el-input>
        </el-form-item>

        <el-form-item label="截止时间" prop="created" label-width="100px">
          <el-input v-model="viewForm.endTime" disabled></el-input>
        </el-form-item>

        <el-form-item label="参与人数" prop="joinCount" label-width="100px">
          <el-input v-model="viewForm.limitCount" type="number" min="1" max="100000000" disabled></el-input>
        </el-form-item>

        <el-form-item label="状态" prop="status" label-width="100px">
          <el-tag size="small" v-if="viewForm.status === 1" type="success">已审核</el-tag>
          <el-tag size="small" v-else-if="viewForm.status === 0" type="warning">未审核</el-tag>
          <el-tag size="small" v-else-if="viewForm.status === 2" type="danger">未通过</el-tag>
        </el-form-item>

        <el-form-item label="未通过理由" prop="reason" label-width="100px">
          <el-input v-model="viewForm.reason" disabled></el-input>
        </el-form-item>

        <el-form-item style="display: flex; justify-content: center;">
          <el-button type="primary" @click="viewDialogVisible = false">关闭</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>


    <!-- 修改活动对话框 -->
    <el-dialog
        title="修改活动"
        :visible.sync="updateDialogVisible"
        width="600px"
        :before-close="handleUpdateClose">
      <el-form :model="viewForm" :rules="editFormActivitys" ref="viewForm">

        <el-form-item label="标题" prop="title" label-width="100px">
          <el-input v-model="viewForm.title" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="主要内容" prop="context" label-width="100px">
          <el-input v-model="viewForm.context" type="textarea" :autosize="{ minRows: 4, maxRows: 10 }"></el-input>
        </el-form-item>

        <!-- 限制该活动的参与人数 -->
        <el-form-item label="参与人数" prop="joinCount" label-width="100px">
          <el-input v-model="viewForm.limitCount" type="number" min="1" max="100000000"></el-input>
        </el-form-item>

        <el-form-item label="地址" prop="address" label-width="100px">
          <el-input v-model="viewForm.address" autocomplete="off"></el-input>
        </el-form-item>

        <!-- 活动开始时间 -->
        <el-form-item label="开始时间" prop="startTime" label-width="100px">
          <el-date-picker type="date" placeholder="选择日期" v-model="viewForm.startTime"
                          style="width: 100%;"></el-date-picker>
        </el-form-item>

        <!-- 活动结束时间 -->
        <el-form-item label="结束时间" prop="endTime" label-width="100px">
          <el-date-picker type="date" placeholder="选择日期" v-model="viewForm.endTime"
                          style="width: 100%;"></el-date-picker>
        </el-form-item>

        <el-form-item label="图片" prop="image" label-width="100px">
          <el-upload
              class="upload-demo"
              :show-file-list="true"
              action="http://localhost:9000/sys/activity/uploadImage"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :on-exceed="handleExceed"
              :before-remove="beforeRemove"
              :file-list="editForm.image"
              multiple
              :limit="3"
              :on-success="handleSuccess">
            <!-- 构建虚拟路径 -->
            <img v-if="viewForm.image" :src=" 'http://localhost:9000/api/file/' + viewForm.image" class="avatar">
            <el-button v-else class="el-icon-plus avatar-uploader-icon">点击上传</el-button>
          </el-upload>
        </el-form-item>

        <el-form-item style="display: flex; justify-content: center;">
          <el-button type="primary" @click="submitUpdateForm('viewForm')">立即修改</el-button>
          <el-button @click="updateDialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 导出活动报名表对话框 -->
    <el-dialog
        title="导出活动报名表"
        :visible.sync="downloadDialogVisible"
        width="600px"
        :before-close="handleDownloadClose">
      <el-form :model="downloadForm" :rules="editFormActivitys" ref="downloadForm">

        <el-form-item label="活动名称" prop="title" label-width="100px">
          <el-input v-model="downloadForm.title" disabled></el-input>
        </el-form-item>

        <el-form-item label="活动内容" prop="context" label-width="100px">
          <el-input v-model="downloadForm.context" type="textarea" :autosize="{ minRows: 4, maxRows: 10 }"
                    disabled></el-input>
        </el-form-item>

        <el-form-item label="活动地址" prop="address" label-width="100px">
          <el-input v-model="downloadForm.address" disabled></el-input>
        </el-form-item>

        <el-form-item label="活动图片" prop="image" label-width="100px" width="100px">
          <img v-if="downloadForm.image" :src=" 'http://localhost:9000/api/file/' + downloadForm.image" class="avatar">
        </el-form-item>

        <el-form-item label="活动开始时间" prop="startTime" label-width="100px">
          <el-input v-model="downloadForm.startTime" disabled></el-input>
        </el-form-item>

        <el-form-item label="活动结束时间" prop="endTime" label-width="100px">
          <el-input v-model="downloadForm.endTime" disabled></el-input>
        </el-form-item>

        <el-form-item style="display: flex; justify-content: center;">
          <el-button type="primary" @click="downloadFile(downloadForm.id)">导出报名表</el-button>
          <el-button @click="downloadDialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 评价活动对话框 -->
    <el-dialog
        title="评价活动"
        :visible.sync="evaluateDialogVisible"
        width="600px"
        :before-close=" handleEvaluateClose">
      <el-form :model="viewForm" :rules="editFormActivitys" ref="evaluateForm">

        <el-form-item label="活动编号" prop="id" label-width="100px">
          <el-input v-model="viewForm.id" disabled></el-input>
        </el-form-item>

        <el-form-item label="活动名称" prop="title" label-width="100px">
          <el-input v-model="viewForm.title" disabled></el-input>
        </el-form-item>

        <el-form-item label="活动评分" prop="score" label-width="100px">
          <el-input type="number" v-model="evaluateForm.score" min="0" max="100"></el-input>
        </el-form-item>

        <el-form-item label="活动评价" prop="comment" label-width="100px">
          <el-input type="textarea" v-model="evaluateForm.evaluation"
                    :autosize="{ minRows: 4, maxRows: 10 }"></el-input>
        </el-form-item>


        <el-form-item style="display: flex; justify-content: center;">
          <el-button type="primary" @click="evaluateSubmit(null)">评价</el-button>
          <el-button @click="evaluateDialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 驳回活动对话框 -->
    <el-dialog
        title="驳回活动"
        :visible.sync="rejectDialogVisible"
        width="600px"
        :before-close="handleRejectClose">
      <el-form :model="editForm" :rules="editFormActivitys" ref="editForm">

        <el-form-item label="驳回理由" prop="rejectReason" label-width="100px">
          <el-input type="textarea" v-model="rejectReason" :autosize="{ minRows: 4, maxRows: 10 }"></el-input>
        </el-form-item>

        <el-form-item style="display: flex; justify-content: center;">
          <el-button type="primary" @click="submitReject()">驳回</el-button>
          <el-button @click="rejectDialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 统计对话框 -->
    <el-dialog
        title="统计"
        :visible.sync="getStatistics"
        width="1000px"
        :before-close="handleClose">

      <!--可以选择时间段到处当年或当月的活动参加情况，统计出最多人参加的活动，最多人报名的活动，以及活动报名率-->
      <el-form :model="editForm" :rules="editFormActivitys" ref="editForm">

        <el-form-item label="开始时间" prop="startTime" label-width="100px">
          <el-date-picker type="date" placeholder="选择日期" v-model="editForm.startTime"></el-date-picker>
        </el-form-item>

        <el-form-item label="结束时间" prop="endTime" label-width="100px">
          <el-date-picker type="date" placeholder="选择日期" v-model="editForm.endTime"></el-date-picker>
        </el-form-item>

        <el-form-item style="display: flex; justify-content: center;">
          <el-button type="primary" @click="statisticsForm('editForm')">立即统计</el-button>
          <el-button @click="resetForm('editForm')">取消</el-button>
        </el-form-item>
      </el-form>

      <!-- 最多人参加的活动 -->
      <div style="margin-top: 20px;">
        <el-card>
          <div slot="header">
            <span>最多人参加的活动</span>
          </div>
          <div style="margin-top: 10px;">
            <el-table :data="mostJoinActivity" style="width: 100%">
              <el-table-column prop="title" label="标题"></el-table-column>
              <el-table-column prop="joinCount" label="参加人数"></el-table-column>
            </el-table>
          </div>
        </el-card>
      </div>

      <!-- 参加活动最多的用户 -->

      <div style="margin-top: 20px;">
        <el-card>
          <div slot="header">
            <span>参加活动最多的用户</span>
          </div>
          <div style="margin-top: 10px;">
            <el-table :data="mostJoinUser" style="width: 100%">
              <el-table-column prop="username" label="用户名"></el-table-column>
              <el-table-column prop="joinCount" label="参加活动数"></el-table-column>
            </el-table>
          </div>
        </el-card>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Activity",
  data() {
    return {
      loading: false,
      searchForm: {},
      delBtlstatus: true,

      total: 0,
      size: 10,
      current: 1,

      evaluateDialogVisible: false,
      downloadDialogVisible: false,
      updateDialogVisible: false,
      viewDialogVisible: false,
      dialogVisible: false,
      getStatistics: false,

      rejectDialogVisible: false, // 控制驳回对话框显示
      rejectReason: '', // 存储驳回理由
      currentId: null, // 当前操作的活动ID

      editForm: {},
      viewForm: {},
      evaluateForm: {
        id: '',
        score: '',
        evaluation: ''
      },
      downloadForm: {},

      tableData: [],
      editFormActivitys: {
        title: [
          {required: true, message: '请输入活动标题', trigger: 'blur'}
        ],
        context: [
          {required: true, message: '请输入主要内容', trigger: 'blur'}
        ],
        address: [
          {required: true, message: '请输入活动地址', trigger: 'blur'}
        ],
        limitCount: [
          {required: true, message: '请输入活动参与人数', trigger: 'blur'}
        ],
        image: [
          {required: true, message: '请上传图片', trigger: 'blur'}
        ],
      },
      mostJoinActivity: [], // 存储最多人参加的活动数据
      mostJoinUser: [],     // 存储参加活动最多的用户
      activityRate: 0,      // 活动的报名率
    }
  },

  created() {
    this.getActivityList()
  },

  methods: {
    // 获取统计数据，根据开始时间和结束时间获取活动参加情况，最多人参加的活动，最多人报名的活动，以及活动报名率
    statisticsForm(formName) {

    },

    // 评价活动前置
    evaluateHandle(id) {
      this.$axios.get(`/sys/activity/view/${id}`)
          .then(res => {
            this.viewForm = res.data.data;
            // 初始化evaluateForm
            this.evaluateForm.id = this.viewForm.id; // 或 res.data.data.id
            this.evaluateForm.score = ''; // 清空之前的评分
            this.evaluateForm.evaluation = ''; // 清空之前的评价
            this.evaluateDialogVisible = true;
          })
          .catch(error => {
            console.error('操作失败', error);
          });
    },

    //评价活动提交表单
    evaluateSubmit() {
      if (!this.evaluateForm.id) {
        this.$message.error('活动ID丢失，请重新进入评价');
        return;
      }
      this.$refs.evaluateForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$axios.post(`/sys/activity/evaluate`, this.evaluateForm)
              .then(res => {
                this.$message.success('评价成功');
                this.evaluateDialogVisible = false;
                this.getActivityList();
              })
              .catch(error => {
                console.error('评价失败', error);
              }).finally(() => {
            this.loading = false;
          });
        }
      });
    },

    //查看活动
    viewHandle(id) {
      this.$axios.get(`/sys/activity/view/${id}`)
          .then(res => {
            this.$message.success('查看成功');
            //将返回的活动数据赋值给viewForm
            this.viewForm = res.data.data;
            console.log("viewForm：" + this.viewForm)
            this.viewDialogVisible = true; // 开启查看对话框
          })
          .catch(error => {
            console.error('操作失败', error);
          });
    },

    // 删除活动
    delHandle(id) {
      var ids = []
      if (id) {
        ids.push(id)
      } else {
        this.multipleSelection.forEach(row => {
          ids.push(row.id)
        })
      }
      console.log(ids)
      this.$axios.post("/sys/activity/delete", ids).then(res => {
        this.$message({
          showClose: true,
          message: '恭喜你，操作成功',
          type: 'success',
          onClose: () => {
            this.getActivityList(); // 刷新列表
          }
        });
      })
    },

    // 审核活动
    auditHandle(id) {
      this.$confirm('确定要通过这个活动的审核吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '驳回',
        type: 'warning'
      }).then(() => {
        this.$axios.post(`/sys/activity/audit/${id}`)
            .then(res => {
              this.$message.success('审核成功');
              this.getActivityList(); // 刷新列表
            })
            .catch(error => {
              console.error('审核出错', error);
            });
      }).catch(() => {
        this.rejectReason = ''; // 清空驳回理由
        this.currentId = id; // 设置当前操作的活动ID
        this.rejectDialogVisible = true; // 显示驳回对话框
      });
    },

    // 提交驳回理由
    submitReject() {
      if (!this.rejectReason) {
        this.$message.error('请填写驳回理由');
        return;
      }
      this.$axios.post(`/sys/activity/auditBat/${this.currentId}`, this.rejectReason, {
        headers: {'Content-Type': 'text/plain'}
      }).then(() => {
        this.$message.success('已驳回');
        this.getActivityList();
        this.rejectDialogVisible = false;
      }).catch(error => {
        console.error('提交驳回失败', error);
      });
    },

    // 用户报名活动
    applicationHandle(id) {
      this.$confirm('确定要报名这个活动吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.post(`/sys/activity/signup/${id}`)
            .then(res => {
              this.$message.success('报名成功');
            })
            .catch(error => {
              console.error('报名失败', error);
            });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消报名'
        });
      });
    },

    // 导出活动数据前置
    downloadHandle(id) {
      this.$axios.get(`/sys/activity/view/${id}`)
          .then(res => {
            //将返回的活动数据赋值给downloadForm
            this.downloadForm = res.data.data;
            console.log("downloadForm：" + this.downloadForm)
            this.downloadDialogVisible = true; // 开启导出报名表对话框
          })
          .catch(error => {
            console.error('操作失败', error);
          });
    },

    // 导出活动报名名单
    downloadFile(activityId) {
      window.open(`http://localhost:9000/sys/activity/export/${activityId}`);
    },

    handleRemove(file, fileList) {
      // 可以在这里添加一些逻辑，比如更新列表状态或调用API删除服务器上的文件
      console.log('移除的文件：', file);
      console.log('当前文件列表：', fileList);
    },
    handlePreview(file) {
      // 如果是图片，可以创建一个窗口预览图片
      if (file.type.startsWith('image/')) {
        // 创建图片预览窗口
        const imgWindow = window.open("", "_blank");
        imgWindow.document.write(`<img src="${file.url || file.raw.preview}" style="width: 100%;">`);
        imgWindow.document.title = file.name;
      } else {
        alert('不支持预览该类型的文件。');
      }
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false
      this.getStatistics = false
      this.editForm = {}
    },
    handleSuccess(response) {
      // 把图片名给img
      console.log("文件上传成功:", response);
      this.editForm.image = response.data;
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val
      this.getRoleList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.current = val
      this.getRoleList()
    },
    handleClose() {
      this.resetForm('editForm')
    },

    handleEvaluateClose() {
      this.viewForm = {}
      this.evaluateDialogVisible = false
    },

    handleRejectClose() {
      this.rejectReason = '';
      this.currentId = null;
      this.rejectDialogVisible = false;
    },


    handleViewClose() {
      this.viewForm = {}
      this.viewDialogVisible = false
    },

    handleUpdateClose() {
      this.updateDialogVisible = false;
    },

    handleDownloadClose(){
      this.downloadDialogVisible = false;
    },

    getActivityList() {
      this.loading = true;
      this.$axios.get("/sys/activity/list", {
        params: {
          keyword: this.searchForm.title,  // 传递搜索关键词至后端
          current: this.current,
          size: this.size
        }
      }).then(res => {
        this.tableData = res.data.data.records;
        this.size = res.data.data.size;
        this.current = res.data.data.current;
        this.total = res.data.data.total;
      }).catch(err => {
        console.error('加载错误', err);
      }).finally(() => {
        this.loading = false;
      });
    },


    // 修改活动前置操作
    updateHandle(id) {
      this.$axios.get(`/sys/activity/view/${id}`)
          .then(res => {
            //将返回的活动数据赋值给viewForm
            this.viewForm = res.data.data;
            console.log("viewForm：" + this.viewForm)
            this.updateDialogVisible = true; // 开启修改对话框
          })
          .catch(error => {
            console.error('操作失败', error);
          });
    },

    // 修改活动提交表单
    submitUpdateForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$axios.post(`/sys/activity/update/${this.viewForm.id}`, this.viewForm)
              .then(res => {
                this.$message({
                  showClose: true,
                  message: '操作成功！',
                  type: 'success'
                });
                this.updateDialogVisible = false; // 关闭对话框
                this.viewForm = {}; // 重置表单
                this.getActivityList(); // 刷新列表
              })
              .catch(error => {
                console.error('操作失败', error);
              }).finally(() => {
            this.loading = false;
          });
        } else {
          console.log('表单验证未通过！')
          return false;
        }
      });
    },

    // 添加活动提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$axios.post("/sys/activity/save", this.editForm)
              .then(res => {
                this.$message({
                  showClose: true,
                  message: '操作成功！',
                  type: 'success'
                });
                this.dialogVisible = false; // 关闭对话框
                this.editForm = {}; // 重置表单
                this.getActivityList(); // 刷新列表
              })
              .catch(error => {
                console.error('操作失败', error);
              }).finally(() => {
            this.loading = false;
          });
        } else {
          console.log('表单验证未通过！')
          return false;
        }
      });
    },

    handleSelectionChange(val) {
      console.log("勾选")
      console.log(val)
      this.multipleSelection = val;

      this.delBtlstatus = val.length == 0
    },
  }
}
</script>

<style scoped>
.el-pagination {
  float: right;
  margin-top: 22px;
}
</style>
